package com.sr.examination.exam.common.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.sr.examination.R;
import com.sr.examination.net.Net;
import com.tl.commonlibrary.network.ErrorResponse;
import com.tl.commonlibrary.network.RequestListener;
import com.tl.commonlibrary.tool.DeviceUtil;
import com.tl.commonlibrary.tool.NumberUnit;

import retrofit2.Call;

/**
 * Created by Administrator on 2017/9/21 0021.
 */

public class PayAlertDialog extends Dialog implements View.OnClickListener {
    private TextView rightBtn, leftBtn;
    private OnAlertClickListener listener;
    private TextView contentTView;
    private Spanned content;
    private long paperId;

    public PayAlertDialog(Context context) {
        super(context, R.style.MyDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_exam_pay);
        Window window = getWindow();
        WindowManager manager = window.getWindowManager();
        Display display = manager.getDefaultDisplay();
        WindowManager.LayoutParams params = window.getAttributes();
        Point point = new Point();
        display.getSize(point);
//        params.height = Math.min(DeviceUtil.dp_to_px(200, getContext()), (int) (point.y * 0.5));
        params.width = Math.min(DeviceUtil.dp_to_px(300, getContext()), (int) (point.y * 2.0 / 3));
        window.setAttributes(params);
        contentTView = (TextView) findViewById(R.id.contentTView);
        if (!TextUtils.isEmpty(content)) {
            contentTView.setText(content);
        }
        rightBtn = (TextView) findViewById(R.id.rightBtn);
        findViewById(R.id.goRechargeBtn).setOnClickListener(this);
        rightBtn.setOnClickListener(this);
        leftBtn = (TextView) findViewById(R.id.leftBtn);
        leftBtn.setOnClickListener(this);
        setCancelable(false);
    }

    public PayAlertDialog setContent(Spanned content) {
        this.content = content;
        if (contentTView != null) {
            contentTView.setText(content);
        }
        return this;
    }

    public PayAlertDialog setMoney(String payCoin, String leftCoin) {
        int color = ContextCompat.getColor(getContext(), R.color.base_red);
        int drawableId = R.mipmap.ic_coin_small;
        String str = "确认支付<font color=" + color + ">" + payCoin + "</font><img src='" + drawableId + "'>打开试卷?<br>当前余额:<font color=" + color + ">" + leftCoin + "</font><img src='" + drawableId + "'>";
        content = Html.fromHtml(str, new Html.ImageGetter() {
            @Override
            public Drawable getDrawable(String source) {
                if (TextUtils.isEmpty(source)) {
                    return null;
                }
                int resId = -1;
                try {
                    resId = Integer.parseInt(source);
                } catch (Exception e) {

                }
                if (resId > 0) {
                    Drawable drawable = ContextCompat.getDrawable(getContext(), resId);
                    if (drawable != null) {
                        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                    }
                    return drawable;
                }
                return null;
            }
        }, null);
        if (contentTView != null) {
            contentTView.setText(content);
        }
        return this;
    }

    public long getPaperId() {
        return paperId;
    }

    public void setPaperId(long paperId) {
        this.paperId = paperId;
    }

    public void setListener(OnAlertClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.rightBtn) {
            dismiss();
            if (listener != null) {
                listener.onRightClicked(v);
            }
        } else if (v.getId() == R.id.leftBtn) {
            dismiss();
            if (listener != null) {
                listener.onLeftClicked(v);
            }
        } else if (v.getId() == R.id.goRechargeBtn) {
            dismiss();
            if (listener != null) {
                listener.onRecharge(v);
            }
        }
    }

    public interface OnAlertClickListener {
        void onRightClicked(View view);

        void onLeftClicked(View view);

        void onRecharge(View view);
    }

}